#pragma once

#include "SharedHandle.h"
#include "SimpleCriticalSection.h"

namespace RoomFW {

class SimpleEvent;
typedef boost::shared_ptr<SimpleEvent> SimpleEventSP;

/// Simple windows event wrapper.  See also http://www.geocities.com/cool_ranju/win32events.html.
///
///   @author	Andrew Sage
class SimpleEvent
{
public:

	/// Constructor
	SimpleEvent(void);

	/// Copy Constructor
	///     @param rhs - Right Hand Side - Object to copy.
	SimpleEvent(const SimpleEvent& rhs);

	/// Destructor
	~SimpleEvent(void);

	/// assignment operator
	///     @param rhs - Right Hand Side - Object to copy.
	SimpleEvent& operator=(const SimpleEvent& rhs);

	/// assignment operator
	///     @param rhs - Right Hand Side - Object to copy.
	SimpleEvent& operator=(const SharedHandle& rhs);

	/// Allow implicit cast to contained HANDLE.
	///   @return handle_
	operator HANDLE() const;

	/// Create the event.
	///     @param manualReset - Create a manual reset event.  See ::CreateEvent().
	///     @param initialState - true for signaled.
	///     @param name - Name for shared event.  May be null.
	///   @return Success.
	bool create(bool manualReset = false, bool initialState = false, const char* name = 0);

	/// Close the event.
	void close(void);

	/// Set the event to signaled.
	///   @return Success.
	bool set(void);

	/// Reset the event to un-signaled.
	///     @return Success.
	bool reset(void);

	/// Wait for the event to be signaled.
	///     @param timeout - Time (in milliseconds) to wait.
	///   @return true if the event is signaled with no errors.
	bool wait(DWORD timeout = INFINITE) const;

private:

	/// Event handle.
	SharedHandle handle_;

	/// Event handle synchronization.
	mutable SimpleCriticalSection cs_;
};

}
